<template>
  <div id="shareTopSearch">
    <h1 class="con-right-title">
      直播类型管理
    </h1>
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="状态" prop="status">
          <el-select v-model="formInline.status" clearable placeholder="请选择">
            <el-option
              v-for="item in [
                { dictLabel: '全部', dictValue: '' },
                ...streamStatus
              ]"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建人" prop="createBy">
          <el-select v-model="formInline.createBy" clearable filterable>
            <el-option
              v-for="(item, index) in personList"
              :key="'item' + index"
              :label="item.nickname"
              :value="item.dapengUserId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="formInline.createTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            icon="el-icon-circle-plus-outline"
            type="success"
            @click="addLiveType"
            >新建一级分类</el-button
          >
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { addLiveType } from '@/api/anchor/live-type'
import { getDpNum } from '@/api/system/user'
export default {
  name: 'shareTopSearch',
  msg: '直播类型列表-顶部搜索',
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          status: '', // 用户类型,默认全部
          collegeId: '', // 学院,默认全部
          createBy: '', //创建人
          startTime: '', // 开始时间
          endTime: '', // 结束时间
          createBy: ''
        }
      }
    },
    streamStatus: {
      type: Array
    }
  },
  data() {
    return {
      // 学院列表
      collegeList: [{ dictLabel: '全部', dictValue: '' }],
      rules: {
        type: {},
        collegeId: {},
        title: {},
        createTime: { type: 'array' }
      },
      // 审核人列表
      personList: [{ nickname: '全部', dapengUserId: '' }]
    }
  },
  watch: {
    'formInline.createTime': {
      handler(newVal, oldVal) {
        if (newVal && newVal.length > 1) {
          this.formInline.startTime = newVal[0]
          this.formInline.endTime = newVal[1]
        } else {
          this.formInline.startTime = ''
          this.formInline.endTime = ''
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getCheckUser()
  },
  methods: {
    // 审核人列表
    getCheckUser() {
      getDpNum({ type: 'OPERATORS' }, (response) => {
        this.personList = this.personList.concat(response)
      })
    },
    // 添加直播类型
    addLiveType() {
      this.$prompt('一级名称：', '新增一级直播分类', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^\S{1,10}$/,
        inputErrorMessage: '输入一级标题不可为空或大于10个字符'
      })
        .then(({ value }) => {
          addLiveType(
            { name: value, status: 'DISABLE' },
            (response) => {
              this.$emit('toSearch')
            },
            (response) => {
              this.$message({
                message: response.message,
                type: 'warning'
              })
            }
          )
        })
        .catch(() => {})
    },
    /* 搜索 */
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
    }
  }
}
</script>
